home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15721 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.6 KB  |  140 lines

  1. Path: ix.netcom.com!news
  2. From: Kugan Kandasamy <kandasam@ix.netcom.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: C++ design question
  5. Date: Fri, 05 Apr 1996 21:35:44 -0800
  6. Organization: Netcom
  7. Message-ID: <316602B0.5DE@ix.netcom.com>
  8. NNTP-Posting-Host: irv-ca7-25.ix.netcom.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-NETCOM-Date: Fri Apr 05 11:37:56 PM CST 1996
  13. X-Mailer: Mozilla 2.0 (Win95; I)
  14.  
  15. This is a C++/design question.
  16.  
  17.     I am attempting to create a "link" between objects by defining
  18. a Link class that stores a pointer to the linked object.  An object can
  19. have as many links as possible.  All classes that use Link are derived
  20. from class Root.
  21.  
  22.     The source looks as follows.  It compiles and runs as expected:
  23.  
  24. // ------------------------------------------------------------------------
  25.  
  26. #include <iostream.h>
  27.  
  28. // All classes that embed Links need to be derived from Root.  See use of
  29. // Root* in the Link class.
  30.  
  31. class Root
  32. {
  33. };
  34.  
  35. // ---------------------------------------------------------------------------
  36.  
  37. template <class T>
  38. class Link 
  39. {
  40.         Root    *m_ptr;
  41. public:
  42.         Link(): m_ptr(0) {}
  43.         Link (Root *ptr) : m_ptr(ptr) {}
  44.     // ~Link() { m_ptr->link.unset(); }
  45.  
  46.         void set(Root *ptr) { m_ptr = ptr; }
  47.         void unset()        { m_ptr = NULL; }
  48.  
  49.         T* operator-> () { return ((T*)m_ptr); }
  50. }; 
  51.  
  52. // ---------------------------------------------------------------------------
  53. // The Connect function connects two objects pointed to by "a" and "b" via
  54. // links "alink" and "blink"
  55.  
  56. template <class T1, class T2>
  57.     void Connect (Root *a, Root *b, Link<T1> &alink, Link<T2> &blink)
  58. {
  59.     alink.set(b);
  60.     blink.set(a);
  61. }
  62.  
  63. // ---------------------------------------------------------------------------
  64. // What follows is user code.
  65.  
  66. class B;
  67.  
  68. class A : public Root
  69. {
  70. public:
  71.     Link<B>    a2b; // Establishes a "port" on which to connect B type object.
  72.     void print() { cout << "In A : " << "\n"; }
  73. };
  74.  
  75. // ---------------------------------------------------------------------------
  76.  
  77. class B : public Root
  78. {
  79. public:
  80.         Link<A>      b2a;  // Establishes a "port" on which to connect A type object.
  81.         void print() { cout << "In B : " << "\n"; }
  82. };
  83.  
  84. // ---------------------------------------------------------------------------
  85.  
  86. main()
  87. {
  88.     A *a = new A;  B *b = new B;
  89.  
  90.     Connect(a, b, a->a2b, b->b2a);
  91.  
  92.         cout << "Before deleting a\n";
  93.         cout << "b->b2a : " << b->b2a.operator->() << endl;
  94.  
  95.     delete a;
  96.  
  97.     cout << "After deleting a\n";
  98.         cout << "b->b2a : " << b->b2a.operator->() << endl;
  99.  
  100.     cout << endl;
  101.  
  102.     return 0;
  103. }
  104.  
  105. ----------------------------------------------------------------------------
  106.  
  107.     I'd like to maintain referential integrity when objects are deleted.
  108. I want to avoid dangling pointers in link objects when a connection is broken.
  109. Since all links are two way, given the mode in which connections are established
  110. with the "Connect" function, I know all the other objects that connect to the
  111. object being deleted.  I want to do something like that shown in the destructor
  112. of the Link class:
  113.  
  114.     ~Link() { m_ptr->link.unset(); }
  115.  
  116.     where "link" is the "port" on the other object that is used to connect
  117. to me.  How can I achieve this?  I realize that this "foreign link" information
  118. must have been supplied at template instantiation time.  So maybe a link is
  119. declared as:
  120.  
  121. class A : public Root
  122. {
  123. public:
  124.         Link<B,b2a> a2b;  // Note two parameters in template class.
  125. };
  126.     
  127.     I cannot, however, define my template Link class like this:
  128.  
  129. template <class T, Link link>
  130. class Link
  131. {
  132.     ....
  133. };
  134.  
  135.     Any ideas will be appreciated.
  136.  
  137.     Regards
  138.     Kugan
  139.     kandasam@ix.netcom.com
  140.